package com.yurun.orgstudentmodule.service.Impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.tendcent.face.sdk.client.TdxSdkClient;
import com.tendcent.face.sdk.client.dto.*;
import com.tendcent.face.sdk.client.resp.TdxSdkResponse;
import com.tendcent.face.sdk.handler.support.Cmd;
import com.yurun.commonapi.api.CommonResult;
import com.yurun.commonapi.dto.StudentNoImportDTO;
import com.yurun.commonapi.pojo.Device;
import com.yurun.commonapi.pojo.Sign;
import com.yurun.commonapi.pojo.Student;
import com.yurun.commonapi.pojo.StudentDocument;
import com.yurun.commonapi.vo.DeviceListVO;
import com.yurun.commonapi.vo.PersonVO;
import com.yurun.commonapi.vo.StudentFaceVO;
import com.yurun.orgstudentmodule.mapper.FaceMapper;
import com.yurun.orgstudentmodule.mapper.StudentDocumentMapper;
import com.yurun.orgstudentmodule.service.FaceService;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import sun.misc.BASE64Encoder;

import javax.servlet.ServletRequest;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;


@Service
public class FaceServiceImpl implements FaceService {
//    private static Logger logger = Logger.getLogger(FaceServiceImpl.class); //    String message = "Hello SLF4J";
//        logger.info("The message is : {}", message);
//
//        logger.info("slf4j for info");
//        logger.debug("slf4j for debug");
//        logger.error("slf4j for error");
//        logger.warn("slf4j for warn");

    @Autowired
    private FaceMapper faceMapper;

    @Autowired
    private StudentDocumentMapper studentDocumentMapper;

    @Override
    public CommonResult addUser(HostInfoDto hostInfo, String deviceKey, String secret, PersonDto personDto) {
        TdxSdkResponse response = TdxSdkClient.addPerson(hostInfo, deviceKey, secret, personDto);
        if (response.getCode().equals("000")) {
            return CommonResult.success("注册用户成功！");
        }
        return CommonResult.failed("注册用户失败！");
    }

    @Override
    public CommonResult delUser(HostInfoDto hostInfo, String deviceKey, String secret, String personId) {
        TdxSdkResponse response = TdxSdkClient.delPerson(hostInfo, deviceKey, secret, personId);
        if (response.getCode().equals("000")) {
            return CommonResult.success("删除用户成功！");
        }
        return CommonResult.failed("删除用户失败！");
    }

    @Override
    public CommonResult addUserFace(HostInfoDto hostInfo, String deviceKey, String secret, FaceDto faceDto) {
        TdxSdkResponse response = TdxSdkClient.addFace(hostInfo, deviceKey, secret, faceDto);
        if (response.getCode().equals("000")) {
            return CommonResult.success("注册用户脸部数据成功！");
        }
        return CommonResult.failed("注册用户脸部数据失败！");
    }

    @Override
    public CommonResult delUserFace(HostInfoDto hostInfo, String deviceKey, String secret, String faceId) {
        TdxSdkResponse response = TdxSdkClient.delFace(hostInfo, deviceKey, secret, faceId);
        if (response.getCode().equals("000")) {
            return CommonResult.success("删除用户脸部数据成功！");
        }
        return CommonResult.failed("删除用户脸部数据失败！");
    }

    @Override
    public CommonResult addPersonPassTime(HostInfoDto hostInfo, String deviceKey, String secret, PersonPasstimeDto personPasstimeDto) {
        TdxSdkResponse response = TdxSdkClient.addPersonPasstime(hostInfo, deviceKey, secret, personPasstimeDto);
        if (response.getCode().equals("000")) {
            return CommonResult.success("设置⼈员通过时间限制成功！");
        }
        return CommonResult.failed("设置⼈员通过时间限制失败！");
    }

    @Override
    public CommonResult delPersonPassTime(HostInfoDto hostInfo, String deviceKey, String secret, String personId) {
        TdxSdkResponse response = TdxSdkClient.delPersonPasstime(hostInfo, deviceKey, secret, personId);
        if (response.getCode().equals("000")) {
            return CommonResult.success("删除⼈员通过时间限制成功！");
        }
        return CommonResult.failed("删除⼈员通过时间限制失败！");
    }

    @Override
    public CommonResult setIdentifyCallback(PersonVO personVO) {
//        System.out.println("personVo："+personVO);
        System.out.println("触发设备识别回调地址方法");
        HostInfoDto hostInfo = new HostInfoDto();
        hostInfo.setHost(personVO.getHost());
        hostInfo.setPort(personVO.getPort());
        hostInfo.setTimeout(personVO.getTimeout());
//        HashMap<String,Object> map = new HashMap<>();
//        TdxSdkClient.test()
        TdxSdkResponse response = TdxSdkClient.setIdentifyCallback(hostInfo, personVO.getDeviceKey(), personVO.getSecret(), personVO.getUrl());
        if (response.getCode().equals("000")) {
            return CommonResult.success("成功设置设备识别回调地址！");
//            map.put("result",1);
////            map.put("code","000");
//            map.put("success","ture");
//            System.out.println(map);
//            return map;
        }
//        map.put("result","设置设备识别回调地址失败！");
//        return map;
        return CommonResult.failed("设置设备识别回调地址失败！");
    }

    @Override
    public String sign(PersonVO personVO) {
//        HostInfoDto hostInfo = new HostInfoDto();
//        hostInfo.setHost(personVO.getHost());
//        hostInfo.setPort(personVO.getPort());
//        hostInfo.setTimeout(personVO.getTimeout());
//        TdxSdkResponse tdxSdkResponse =TdxSdkClient.test(hostInfo,personVO.getDeviceKey(),personVO.getSecret());
//        System.out.println(tdxSdkResponse);
//        System.out.println("personVo："+personVO);
        Sign sign = new Sign();
        long l = Long.parseLong(personVO.getTime());
        Date date = new Date(l);
        System.out.println("打卡机传来的时间为:"+date);
        sign.setSignDate(new Date(l));
        sign.setStudentId(BigInteger.valueOf(Long.parseLong(personVO.getPersonId())));
        sign.setCreateTime(new Date(l));
        sign.setUpdateTime(new Date(l));
        sign.setIsDel(0);
//        System.out.println(personVO.getTemperature()+"温度");
        sign.setSignTemperature(Float.parseFloat(personVO.getTemperature()));
//        System.out.println(sign);
        System.out.println("进来--------");
        // 查询今日是否签到
        Sign flag = faceMapper.getOneSign(sign);
        System.out.println("今日flag是"+flag);
        if (flag == null) { // 未签到
            sign.setSignFist(new Date(l));
            faceMapper.sign(sign);
            System.out.println(personVO.getPersonId()+"："+personVO.getName()+"   第一次签到成功："+sign.getSignFist());
        } else {
            sign.setSignLast(new Date(l));
            sign.setUpdateTime(new Date(l));
            faceMapper.updateSign(sign);
            System.out.println(personVO.getPersonId()+"："+personVO.getName()+"   第二次签到成功："+sign.getSignLast());
        }
//        HashMap<String,Object> map = new HashMap<>();
//        map.put("result",1);
////        map.put("code","000");
//        map.put("success","ture");
//        System.out.println(map);
//        return map;
//        return null;
//        System.out.println("{\"result\":1,\"success\":ture}");
        System.out.println("这个记录的时间是："+personVO.getTime());
        return "{\"result\":1,\"success\":ture}";
    }

    @Override
    public CommonResult addSign(Sign sign){

        Sign flag = faceMapper.getOneSignInsert(sign);
        System.out.println("---------今日flag是-----------"+flag);
        if (flag == null) { // 未签到

            faceMapper.sign(sign);
        } else {
            System.out.println("1------------覆盖--------");
            sign.setUpdateTime(new Date());
            faceMapper.updateSignInsert(sign);
        }

            return CommonResult.success("新增成功！");

    }

    @Override
    public CommonResult addPerson(PersonVO personVO) {
        List<Device> deviceList = faceMapper.getMoreDeviceList(personVO);
        //Device device = faceMapper.getOneDevice(personVO);
        for (Device device : deviceList) {
            HostInfoDto hostInfoDto = new HostInfoDto();
            hostInfoDto.setHost(device.getHost());
            hostInfoDto.setTimeout(device.getTimeout());
            hostInfoDto.setPort(device.getPort());
//      System.out.println(personDto.getId()+personDto.getName());
            //TdxSdkClient.setIdentifyCallback(hostInfoDto, personVO.getDeviceKey(), personVO.getSecret(), "http://192.168.5.12:8888/admin/signManage/sign");
            PersonDto personDto = new PersonDto();
            personDto.setId(personVO.getPersonId());
            personDto.setName(personVO.getName());
            personDto.setIdcardNum(personVO.getPersonId());

            JSONArray jsonArray = JSONArray.parseArray(personVO.getUrl());
            FaceDto faceDto = new FaceDto();

            faceDto.setImgBase64(imgToBase64(String.valueOf(jsonArray.get(0))));
            faceDto.setFaceId(personVO.getPersonId());
            faceDto.setPersonId(personVO.getPersonId());

            TdxSdkResponse response = TdxSdkClient.addPerson(hostInfoDto, device.getDeviceKey(), device.getSecret(), personDto);
//            System.out.println(response+"-------"+response.getCode());
            if (response.getCode().equals("000")) {
                StudentDocument studentDocument = new StudentDocument();
                studentDocument.setIsImport(1);
                studentDocument.setStudentId(BigInteger.valueOf(Long.parseLong(personVO.getPersonId())));
                studentDocumentMapper.updateIsImport(studentDocument);  // 这里后期需要加个事务，防止更新失败导致与考勤机器不一致！
                TdxSdkResponse res = TdxSdkClient.addFace(hostInfoDto, device.getDeviceKey(), device.getSecret(), faceDto);

                if (res.getCode().equals("000")) {
                    return CommonResult.success("新增成功！");
                }
                return CommonResult.failed("新增照片失败，请去考勤机录入！");
            }
            return CommonResult.failed("增加学生失败，请检查后重试！");
        }
        return CommonResult.failed("增加学生失败，请检查后重试！");
    }

    public String addPerson1(PersonVO personVO) {

        HostInfoDto hostInfoDto = new HostInfoDto();
        hostInfoDto.setHost("192.168.5.12");
        hostInfoDto.setTimeout(900000);
        hostInfoDto.setPort(10022);

        //TdxSdkClient.setIdentifyCallback(hostInfoDto, personVO.getDeviceKey(), personVO.getSecret(), "http://192.168.5.12:8888/admin/signManage/sign");
        PersonDto personDto = new PersonDto();
        personDto.setId(personVO.getPersonId());
        personDto.setName(personVO.getName());
        personDto.setIdcardNum(personVO.getPersonId());
        System.out.println(hostInfoDto.getHost());
        System.out.println(personDto.getId());


        FaceDto faceDto = new FaceDto();
        faceDto.setImgBase64(personVO.getImgBase64());
        faceDto.setFaceId(personVO.getPersonId());
        faceDto.setPersonId(personVO.getPersonId());

        TdxSdkResponse response = TdxSdkClient.addPerson(hostInfoDto, personVO.getDeviceKey(), personVO.getSecret(), personDto);
        System.out.println(response.getCode());
        if (response.getCode().equals("000")) {
            if (TdxSdkClient.addFace(hostInfoDto, personVO.getDeviceKey(), personVO.getSecret(), faceDto).getCode().equals("000")) {
                return "成功";
            }
            return "照片失败";
        }
        return "加人失败";
    }

    @Override
    public CommonResult addDevice(PersonVO personVO) {
        Device device = new Device();
        device.setOrganizationId(personVO.getOrganizationId());
        device.setCreateTime(new Date());
        device.setUpdateTime(new Date());
        device.setIsDel(0);
        BeanUtils.copyProperties(personVO, device);
        HostInfoDto hostInfo = new HostInfoDto();
        hostInfo.setHost(personVO.getHost());
        hostInfo.setPort(personVO.getPort());
        hostInfo.setTimeout(personVO.getTimeout());

        DeviceConfigDto deviceConfigDto = new DeviceConfigDto();
        //deviceConfigDto.setCompanyName("特殊儿童教育中心");
        TdxSdkResponse re = TdxSdkClient.configDevice(hostInfo, personVO.getDeviceKey(), personVO.getSecret(), deviceConfigDto);
        TdxSdkResponse response = TdxSdkClient.setIdentifyCallback(hostInfo, personVO.getDeviceKey(), personVO.getSecret(), personVO.getUrl());
        System.out.println(re.getCode() + "___" + response.getCode());
        if (re.getCode().equals("000") && response.getCode().equals("000")) {
            int flag = faceMapper.addDevice(device);
            if (flag == 1) {
                return CommonResult.success("新增成功！");
            }
        }
        return CommonResult.failed("新增失败！");
    }

    @Override
    public CommonResult getDeviceList(DeviceListVO deviceListVO) {
        /*List<StudentFaceVO> list1 =  faceMapper.getAll();
        System.out.println("数组"+list1.size());
        for (StudentFaceVO s : list1) {
            //StudentFaceVO s = list1.get(i);

        //for (StudentFaceVO s : list1) {

            String personId = String.valueOf(s.getStudentId());
            PersonVO personVO = new PersonVO();
            personVO.setHost("192.168.5.12");
            personVO.setDeviceKey("C8AA2C582B9B1AA8");
            personVO.setSecret("houhouquan");
            personVO.setPersonId(personId);
            personVO.setPort(10022);
            personVO.setName(s.getName());
            personVO.setTimeout(900000);

            JSONArray jsonArray = JSONArray.parseArray(s.getUrl());
            String url = "";
            if (jsonArray.size()==0) {
                System.out.println("进来默认");
                url = "http://i.qqkou.com/i/1a1819319395x1882313067b26.jpg";
            } else {
                url = String.valueOf(JSONObject.parseObject(String.valueOf(jsonArray.get(0))).get("url"));
            }
            //System.out.println("+++"+url+"__"+url.equals(" "));

            *//*String s1 = url.substring(0, 5);
            String s2 = url.substring(5);
            String url1 = s1+""+s2;*//*
         *//*String url2 = url.replace("/", "\\");
            String s1 = url2.substring(0, 5);
            String s2 = url2.substring(5);
            String url1 = s1+"\\"+s2;
            System.out.println(url2);*//*
            personVO.setImgBase64(imgToBase64(url));
            //System.out.println(personVO.getImgBase64());
            //if(addPerson1(personVO)) {
                System.out.println(addPerson1(personVO));
//            } else {
//                System.out.println("失败");
//            }
        }*/

        PageHelper.startPage(deviceListVO.getPageNum(), deviceListVO.getPageSize());
        List<Device> list = faceMapper.getDeviceList(deviceListVO);
        Map<String, Object> map = new HashMap<>();
        map.put("total", list.size());
        map.put("data", zljPageHelper(list, deviceListVO.getPageNum(), deviceListVO.getPageSize()));
        return CommonResult.success(map);
    }

    @Override
    public CommonResult deleteDevice(Device device) {
        device.setUpdateTime(new Date());
        if (faceMapper.deleteDevice(device) == 1) return CommonResult.success("删除成功！");
        return CommonResult.failed("删除失败！");
    }

    @Override
    public CommonResult updateDevice(Device device) {
        device.setUpdateTime(new Date());
        PersonVO personVO = new PersonVO();
        personVO.setOrganizationId(device.getOrganizationId());
        personVO.setBranchId(device.getBranchId());
        Device temp = faceMapper.getOneDevice(personVO);

        HostInfoDto hostInfo = new HostInfoDto();
        hostInfo.setHost(device.getHost());
        hostInfo.setPort(device.getPort());
        hostInfo.setTimeout(device.getTimeout());

        if (!temp.getUrl().equals(device.getUrl())) {
            TdxSdkResponse response = TdxSdkClient.setIdentifyCallback(hostInfo, device.getDeviceKey(), device.getSecret(), device.getUrl());
            if (response.getCode().equals("000") && faceMapper.updateDevice(device) == 1) {
                return CommonResult.success("更新成功！");
            }
        }
        return CommonResult.failed("更新失败！");
    }

    @Override
    public CommonResult getNoImport(BigInteger organizationId, BigInteger branchId, int pageNum, int pageSize) {
        List<StudentNoImportDTO> list = studentDocumentMapper.getNoImport(organizationId, branchId);
        Map<String, Object> map = new HashMap<>();
        map.put("total", list.size());
        map.put("data", zljPageHelper1(list, pageNum, pageSize));
        return null;
    }

    @Override
    public CommonResult addMorePerson(BigInteger organizationId, BigInteger branchId) {
        // 获取该分店设备信息
        PersonVO personVO = new PersonVO();
        personVO.setOrganizationId(organizationId);
        personVO.setBranchId(branchId);
        Device device = faceMapper.getOneDevice(personVO);
        personVO.setTimeout(device.getTimeout());
        personVO.setPort(device.getPort());
        personVO.setSecret(device.getSecret());
        personVO.setDeviceKey(device.getDeviceKey());
        personVO.setHost(device.getHost());

        // 获取分店未导入的学生
        List<StudentNoImportDTO> list = studentDocumentMapper.getNoImport(organizationId, branchId);
        for (StudentNoImportDTO studentNoImportDTO : list) {
            personVO.setPersonId(String.valueOf(studentNoImportDTO.getStudentId()));
            personVO.setIdcardNum(String.valueOf(studentNoImportDTO.getStudentId()));
            personVO.setName(studentNoImportDTO.getStudentName());
            JSONArray jsonArray = JSONArray.parseArray(studentNoImportDTO.getStudentPicMe());
            String[] str = new String[1];
            str[0] = String.valueOf(JSONObject.parseObject(String.valueOf(jsonArray.get(0))).get("url"));
            personVO.setUrl(Arrays.toString(str));
            addPerson(personVO);
        }
        return null;
    }

    @Override
    public CommonResult importStuInfo(BigInteger organizationId, BigInteger branchId) {
        List<StudentFaceVO> list1 = faceMapper.getAll(organizationId, branchId);
        PersonVO personVO = new PersonVO();
        personVO.setBranchId(branchId);
        personVO.setOrganizationId(organizationId);
        List<Device> deviceList = faceMapper.getMoreDeviceList(personVO);
        List<String> failList = new ArrayList<>();
//        System.out.println("学生列表"+list1);
//        System.out.println("设备列表"+deviceList);
//        failList.add(s.getStudentId());
//                    while (list1.size() != 0) {
        Map<String, Object> map = new HashMap<>();
        map.put("url", "http://120.79.186.60:8887/file/image/6386d220-ed40-4088-8a6d-ef03b175dcda.PNG");
        map.put("uid","1653744970098");
        map.put("status","success");
        String jsonString = JSON.toJSONString(map);
        for (Device device : deviceList) {
            HostInfoDto hostInfoDto = new HostInfoDto();
            hostInfoDto.setHost(device.getHost());
            hostInfoDto.setTimeout(device.getTimeout());
            hostInfoDto.setPort(device.getPort());
            for (StudentFaceVO s : list1) {

                System.out.println(s);

                if (Objects.equals(s.getUrl(), "[]") || s.getUrl().length() == 0){
                    s.setUrl("["+jsonString+"]");
                    System.out.println("执行了");
                }
                System.out.println("后面的"+s);

                PersonDto personDto = new PersonDto();
                personDto.setId(String.valueOf(s.getStudentId()));
                personDto.setName(s.getName());
                personDto.setIdcardNum(String.valueOf(s.getStudentId()));

                JSONArray jsonArray = JSONArray.parseArray(s.getUrl());

                FaceDto faceDto = new FaceDto();
                faceDto.setImgBase64(imgToBase64(String.valueOf(JSONObject.parseObject(String.valueOf(jsonArray.get(0))).get("url"))));
                faceDto.setFaceId(String.valueOf(s.getStudentId()));
                faceDto.setPersonId(String.valueOf(s.getStudentId()));

                TdxSdkResponse response = TdxSdkClient.addPerson(hostInfoDto, device.getDeviceKey(), device.getSecret(), personDto);
                System.out.println("第一个" + response);
                if (response.getCode().equals("000")) {
                    StudentDocument studentDocument = new StudentDocument();
                    studentDocument.setIsImport(1);
                    studentDocument.setStudentId(BigInteger.valueOf(Long.parseLong(String.valueOf(s.getStudentId()))));
                    studentDocumentMapper.updateIsImport(studentDocument);  // 这里后期需要加个事务，防止更新失败导致与考勤机器不一致！
                    TdxSdkResponse res = TdxSdkClient.addFace(hostInfoDto, device.getDeviceKey(), device.getSecret(), faceDto);
//                    System.out.println("第二个" + res);
                    if (res.getCode().equals("000")) {
//                        return CommonResult.success("新增成功！");
                        System.out.println(s.getStudentId() + "导入成功");
                    } else {
                        System.out.println(s.getStudentId() + "导入失败");
                        failList.add(s.getStudentId()+s.getName());
                    }
//                    return CommonResult.failed("新增照片失败，请去考勤机录入！");

                }
//                (else {
//                    failList.add(s.getStudentId());
//                    while (failList.size() != 0) {
//                        for (Device d : deviceList) {
//                            for (StudentFaceVO t : list1) {
//                                PersonDto personDto1 = new PersonDto();
//                                personDto1.setId(String.valueOf(t.getStudentId()));
//                                personDto1.setName(t.getName());
//                                personDto1.setIdcardNum(String.valueOf(t.getStudentId()));
//
//                                JSONArray jsonArray1 = JSONArray.parseArray(t.getUrl());
//                                FaceDto faceDto1 = new FaceDto();
//
//                                faceDto.setImgBase64(imgToBase64(String.valueOf(JSONObject.parseObject(String.valueOf(jsonArray.get(0))).get("url"))));
//                                faceDto.setFaceId(String.valueOf(t.getStudentId()));
//                                faceDto.setPersonId(String.valueOf(t.getStudentId()));
//
//                                TdxSdkResponse response1 = TdxSdkClient.addPerson(hostInfoDto, device.getDeviceKey(), device.getSecret(), personDto);
////
//                                if (response1.getCode().equals("000")) {
//                                    StudentDocument studentDocument = new StudentDocument();
//                                    studentDocument.setIsImport(1);
//                                    studentDocument.setStudentId(BigInteger.valueOf(Long.parseLong(String.valueOf(s.getStudentId()))));
//                                    studentDocumentMapper.updateIsImport(studentDocument);  // 这里后期需要加个事务，防止更新失败导致与考勤机器不一致！
//                                    TdxSdkResponse res = TdxSdkClient.addFace(hostInfoDto, device.getDeviceKey(), device.getSecret(), faceDto);
//
//                                    if (res.getCode().equals("000")) {
//                                        return CommonResult.success("新增成功！");
//                                    }
//                                    return CommonResult.failed("新增照片失败，请去考勤机录入！");
//                                }
//                            }
//                        }
//                        return CommonResult.failed("增加学生失败，请检查后重试！");
//                    }
//                })

//
//                }
            }
        }
        System.out.println(failList);
        return CommonResult.success("新增成功！");
    }


    public List<Device> zljPageHelper(List<Device> list, int pageNum, int pageSize) {
        List<Device> temp = new ArrayList<>();

        for (int i = pageSize * (pageNum - 1); i < list.size() && i < (pageSize * (pageNum - 1) + pageSize); i++) {
            temp.add(list.get(i));
        }
        return temp;
    }

    public List<StudentNoImportDTO> zljPageHelper1(List<StudentNoImportDTO> list, int pageNum, int pageSize) {
        List<StudentNoImportDTO> temp = new ArrayList<>();

        for (int i = pageSize * (pageNum - 1); i < list.size() && i < (pageSize * (pageNum - 1) + pageSize); i++) {
            temp.add(list.get(i));
        }
        return temp;
    }

    /**
     * 图片转base64字符串
     *
     * @param imgFile 图片路径
     * @return
     */
    public static String imageToBase64Str(String imgFile) {
        InputStream inputStream = null;
        byte[] data = null;
        try {
            inputStream = new FileInputStream(imgFile);
            data = new byte[inputStream.available()];
            inputStream.read(data);
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 加密
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);
    }

    /**
     * 服务器图片转换base64 path：服务器图片路径  返回 base64编码（String 类型）
     *
     * @param path
     * @return
     */
    public static String imgToBase64(String path) {

        byte[] data = null;
        InputStream in = null;
        ByteArrayOutputStream out = null;
        try {
            URL url = new URL(path);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setConnectTimeout(50000);
            connection.setReadTimeout(50000);
            in = connection.getInputStream();
            out = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int len = 0;
            while ((len = in.read(b)) != -1) {
                out.write(b, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
                e.getStackTrace();
            }
        }
        System.out.println("转换后的图片大小：" + out.toByteArray().length / 1024);
        if (out.toByteArray().length / 1024 > 500) {
            return null;
        }
        BASE64Encoder base = new BASE64Encoder();
        return base.encode(out.toByteArray());
    }

    /**
     * 定时任务，每天定时检查是否有学生未录入
     */
    //@Scheduled(fixedRate = 86400)
    public void timeTask() {

    }


}
